<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Marketing;


use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Good;
use App\Models\JinGangAnew;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class BannerController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Marketing\BannerController";

    public function index(Request $request)
    {
        $pageSize = $request->pageSize;
        $sql = Banner::select('id', 'type', 'image', 'sec', 'createtime', 'isIndex');
        $bannerList = $sql->orderBy('isIndex', 'desc')
            ->orderBy('indexTime', 'desc')
            ->orderByDesc('createtime')
            ->paginate($pageSize)
            ->toArray();
        $data['bannerList'] = $bannerList['data'];
        foreach ($data['bannerList'] as $key => $value) {
            $data['bannerList'][$key]['image'] = path($value['image']);
            if (isset($value['createtime'])) {
                $data['bannerList'][$key]['createtime'] = date("Y-m-d H:i:s", $value['createtime']);
            }
        }
        $data['totalNum'] = $bannerList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $banner = Banner::select('t1.id', 't1.type', 't1.image', 't1.sec', 't1.url', 't2.id as goods_id', 't2.name as goods_name', 't3.id as jingang_id', 't3.name as jingang_name')
            ->from((new Banner())->getTable() . ' as t1')
            ->leftJoin((new Good())->getTable() . ' as t2', 't1.goods_id', '=', 't2.id')
            ->leftJoin((new JinGangAnew())->getTable() . ' as t3', 't1.jingang_id', '=', 't3.id')
            ->where('t1.id', $id)
            ->first();
        $banner['image'] = path($banner['image']);
        return $this->apiResponse($banner);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('type', 'image', 'sec', 'url', 'goods_id', 'jingang_id');
        if (!isset($data['type']) || !isset($data['image']) || !isset($data['sec']) || !$data['sec']) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $num = 0;
        if ($data['type'] == 1) {
            if (!isset($data['goods_id'])) {
                return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
            }
            $good = Good::where('id', $data['goods_id'])->where('status', 1)->where('is_del', 0)->first();
            if (!$good) {
                return $this->apiResponse('', config('errorLiu.SHANGPONINBUCUNAZAIDEHI'));
            }
            $num = Banner::where('id', '!=', $id)->where('goods_id', $data['goods_id'])->count();
        } else if ($data['type'] == 2) {
            if (!isset($data['url'])) {
                return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
            }
        } else if ($data['type'] == 4) {
            if (!isset($data['jingang_id'])) {
                return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
            }
            $num = Banner::where('id', '!=', $id)->where('jingang_id', $data['jingang_id'])->count();
        } else {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        if ($num) {
            return $this->apiResponse('', config('errorLiu.GAIBEIRONGXIALUNBOTUDEIDNIN'));
        }
        if ($id) {
            $content = "修改";
            Banner::where('id', $id)->update($data);
        } else {
            $content = "新增";
            $data['createtime'] = time();
            $id = Banner::insertGetId($data);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的首页轮播图,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "首页轮播图模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $banner = Banner::where('id', $id)->first();
//        if ($banner['isIndex']) {
//            Banner::where('id', $id)->update([
//                'isIndex' => 0,
//                'indexTime' => null
//            ]);
//            $content = "禁用";
//        } else {
//            Banner::where('id', $id)->update([
//                'isIndex' => 1,
//                'indexTime' => date("Y-m-d H:i:s")
//            ]);
//            $content = "启用";
//        }
        Banner::where('id', $id)->update([
            'isIndex' => 1,
            'indexTime' => date("Y-m-d H:i:s")
        ]);
        $content = "置顶";
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content .= "了id为【".$id."】的首页轮播图";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "首页轮播图模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $banner = Banner::select()->where('id', $id)
            ->first();
        Banner::where('id', $id)->delete();
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的首页轮播图,内容为【".json_encode($banner)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "首页轮播图模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function goodList(Request $request)
    {
        $id = $request->id;
        $name = $request->name;
        $sql = Good::select('t1.id', 't1.name')
            ->from((new Good())->getTable() . ' as t1')
            ->leftJoin((new Banner())->getTable() . ' as t2', 't2.goods_id', '=', 't1.id')
            ->where('t1.is_del', 0)
            ->where('t1.status', 1)
            ->where('t2.id', null);
        if (isset($id)) {
            $sql->where('t1.id', $id);
        }
        if (isset($name)) {
            $sql->where('t1.name', 'like', "%$name%");
        }
        $goodList = $sql->orderBy('t1.createtime', 'desc')
            ->get()
            ->toArray();
        return $this->apiResponse($goodList);
    }

    public function jinList(Request $request)
    {
        $id = $request->id;
        $name = $request->name;
        $sql = JinGangAnew::select('t1.id', 't1.name')
            ->from((new JinGangAnew())->getTable() . ' as t1')
            ->leftJoin((new Banner())->getTable() . ' as t2', 't2.jingang_id', '=', 't1.id')
            ->where('t2.id', null);
        if (isset($id)) {
            $sql->where('t1.id', $id);
        }
        if (isset($name)) {
            $sql->where('t1.name', 'like', "%$name%");
        }
        $jinList = $sql->orderBy('t1.createtime', 'desc')
            ->get()
            ->toArray();
        return $this->apiResponse($jinList);
    }


}